package persistent.impl;

import java.security.interfaces.RSAKey;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.omg.CORBA.PUBLIC_MEMBER;

import basic.BaseDAO;
import model.TForumConent;
import model.VForumContent;
import persistent.dao.ForumContentDAO;

public class ForumContentDAOImpl implements ForumContentDAO {

	private BaseDAO dao=null;
	
	public ForumContentDAOImpl() {
		super();
		dao=new BaseDAO();
	}
	@Override
	public int produceValidSortId(int forumid) {
		int row = 0;
		String sql="select max(ordernum) from T_Forumcontent where forumid=?";
		Object[] param={forumid};
		ResultSet rs=dao.select(sql, param);
		try {
			rs.next();
			 row=rs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		dao.close();
		
		
		return row+1;
	}

	@Override
	public int addContent(TForumConent fcontent) {
	String sql="up_addForumContent(?,?,?,?,?)";
	Object[] param={
			fcontent.getForumid(),
			fcontent.getPhotocontent(),
			fcontent.getTextcontent(),
			fcontent.getOrdernum(),
			fcontent.getIsrecommand()};
	 int row=(Integer)dao.executeSelectProduce(sql, param);
	dao.close();
		return row;
	}

	@Override
	public boolean deleteContent(int id) {
		String sql="up_deleteForumContent(?)";
		Object[] param={id};
		if((Integer)dao.executeSelectProduce(sql, param)>0){
			return true;
		}else
				return false;
	}

	@Override
	public boolean deleteContentsBelongForum(int forumid) {
		String sql="delete from T_Forumcontent where forumid=?";
		Object[] param={forumid};
		if(dao.delete(sql, param)>0){
			return true;
		}else
				return false;
	}

	@Override
	public boolean updateForumContent(TForumConent fcontent) {
		String sql="up_updateForumContent(?,?,?,?,?,?)";
		Object[] param={fcontent.getContentid(),fcontent.getForumid(),fcontent.getPhotocontent()
				,fcontent.getTextcontent(),fcontent.getOrdernum(),fcontent.getIsrecommand()};
		if((Integer)dao.executeSelectProduce(sql, param)>0){
			return true;
		}else
				return false;
	}
	

	@Override
	public VForumContent getContnentById(int forumid)  {
		String sql="select * from V_ForumContent where contentid=?";
		Object[] param={forumid};
		ResultSet rs=dao.select(sql, param);
		try {
			rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		VForumContent VF=new VForumContent(rs);
		dao.close();
		return VF;
	}

	@Override
	public TForumConent getTContnetById(int forumid)  {
		String sql="select * from T_Forumcontent where contentid=?";
		Object[] param={forumid};
		ResultSet rs=dao.select(sql, param);
		try {
			rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TForumConent TF=new TForumConent(rs);
		dao.close();
		return TF;
		
	}

	@Override
	public List<VForumContent> getContentListByForumId(int forumid) {
		String sql="select*from V_ForumContent where forumid=?";
		Object[] param={forumid};
		ResultSet rs=dao.select(sql, param);
		List<VForumContent> list=VForumContent.toList(rs);
		dao.close();
		return list;
	}

}
